package com.example.shortmessage.base;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shortmessage.R;
import com.example.shortmessage.data.constant.FontsConst;
import com.example.shortmessage.utils.DisplayUtil;

public class BaseStyle {
    private Context context;

    public void set(Context context, Object control, String form) {
        this.context = context;
        float px = getDimens(form);
        setSize(control, px);
    }

    /**
     *
     * @param form   {@link FontsConst}
     * @return sp、dp
     */
    private float getDimens(String form){
        float dimen = 0;
        if(form.equals(FontsConst.TV_CONTENT)){
            dimen = getTvContent();
        }else if(form.equals(FontsConst.TV_TITLE)){
            dimen = getTvTitle();
        }else if(form.equals(FontsConst.TV_MIN_CONTENT)){
            dimen = getTvMinContent();
        }
        return DisplayUtil.px2sp(context,dimen);
    }

    private void setSize(Object control, float dimens) {
        if (control instanceof TextView) {
            ((TextView) control).setTextSize(dimens);
        } else if (control instanceof EditText) {
            ((EditText) control).setTextSize(dimens);
        } else if (control instanceof Button) {
            ((Button) control).setTextSize(dimens);
        } else if (control instanceof ImageView) {
            ImageView iv = ((ImageView) control);
            ViewGroup.LayoutParams layoutParams = iv.getLayoutParams();
            layoutParams.height = (int)(dimens+0.5f);
            layoutParams.width = (int)(dimens+0.5f);
            iv.setLayoutParams(layoutParams);
        }
    }

    /**
     *
     * @return PX
     */
    private float getTvContent(){
        return context.getResources().getDimension(R.dimen.tv_content);
    }

    private float getTvTitle(){
        return context.getResources().getDimension(R.dimen.tv_title);
    }

    private float getTvMinContent() {
        return context.getResources().getDimension(R.dimen.tv_min_content);
    }
}
